package com.jgsu.farm.mapper.sql;

import com.jgsu.farm.entity.Enemy;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public interface EnemyMapperSql {
    @Insert("insert into enemy(enemyID,chineseName,latinName,alias,category,distribution,morphology,imageID,lifeHabit) values(#{enemyid},#{chinesename},#{latinname},#{alias},#{category},#{distribution},#{morphology},#{imageid},#{lifehabit})")
    @Options(useGeneratedKeys = true, keyProperty = "enemyid", keyColumn = "enemyID")
    int insert(Enemy enemy);

    @Delete("delete from enemy where enemyID = #{enemyid}")
    int delete(Integer enemyid);

    @Select("select * from enemy where enemyID = #{enemyid}")
    Enemy selectById(Integer enemyid);

    @Results(id = "enemyMap", value = {
            @Result(id = true, column = "enemyid", property = "enemyid"),
            @Result(column = "chinesename", property = "chinesename"),
            @Result(column = "latinname", property = "latinname"),
            @Result(column = "alias", property = "alias"),
            @Result(column = "category", property = "category"),
            @Result(column = "category", property = "biologyCategory", one = @One(select = "com.jgsu.farm.mapper.sql.BiologyMapperSql.selectCategoryById", fetchType = FetchType.EAGER)),
            @Result(column = "distribution", property = "distribution"),
            @Result(column = "morphology", property = "morphology"),
            @Result(column = "imageid", property = "imageid"),
            @Result(column = "imageid", property = "matchImageList", many = @Many(select = "com.jgsu.farm.mapper.sql.MatchImageMapperSql.findMatchImageByPlantId", fetchType = FetchType.LAZY)),
            @Result(column = "lifehabit", property = "lifehabit"),
    })
    @Select("select * from enemy")
    List<Enemy> selectAll();

    @Update("update enemy set chineseName = #{chinesename},latinName = #{latinname},alias = #{alias},category = #{category},distribution = #{distribution},morphology = #{morphology},imageID = #{imageid},lifeHabit = #{lifehabit} where enemyID = #{enemyid}")
    void update(Enemy enemy);
}
